//
//  restaurantVC.m
//  dianfan
//
//  Created by Ye Jin on 15/11/13.
//  Copyright © 2015年 Ye Jin. All rights reserved.
//

#import "restaurantVC.h"
#import <BmobSDK/bmob.h>
#import "MenuVC.h"
#import "logoutVC.h"
#import "MBProgressHUD.h"
#import "Restaurant.h"

@interface restaurantVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic) NSMutableArray *restaurants;

@end

@implementation restaurantVC
- (IBAction)pageRefresh:(id)sender {
    [self viewWillAppear:YES];
}

- (void)logout
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    logoutVC *lvc = [sb instantiateViewControllerWithIdentifier:@"logout"];
    [self.navigationController pushViewController:lvc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"饭店";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //转菊花
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果
    hud.labelText=@"loading";
    self.restaurants = [[NSMutableArray alloc] init];
    
    BmobUser *bUser = [BmobUser getCurrentUser];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:bUser.username
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(logout)];
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"Restaurant"];
    //查找Restaurant表的数
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@",[[error userInfo] objectForKey:@"error"]] message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        for (BmobObject *obj in array) {
            Restaurant *restaurant = [[Restaurant alloc] init];
            restaurant.ID = [obj objectForKey:@"objectId"];
            restaurant.name = [obj objectForKey:@"restaurantName"];
            [self.restaurants addObject:restaurant];
        }
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.restaurants count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"restaurant";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    Restaurant *restaurant = [self.restaurants objectAtIndex:indexPath.row];
    cell.textLabel.text = restaurant.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MenuVC *mvc = [[MenuVC alloc] init];
    mvc.restaurant = [self.restaurants objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:mvc animated:YES];
}

@end
